<?php
header("Content-Type: application/json; charset=UTF-8");

include_once "service.php";
$service = new Dcservice();

$dish_name = $_REQUEST['dish_name'];
$price = $_REQUEST['price'];
$dish_type_id = $_REQUEST['dish_type_id'];

$icon = $_FILES['icon'];
$fileName = str_replace(".", "", microtime(true));
$fileExc = pathinfo($icon["name"])['extension'];
$file = './img/' . $fileName . "." . $fileExc;

copy($icon["tmp_name"], $file);

$result = ["code" => 0, "msg" => "添加成功！"];
try {
    $service->addDish($dish_name, $price, $dish_type_id, $file);
} catch (Error $error) {
    $result = ["code" => -1, "msg" => $error->getMessage()];
}
echo json_encode($result);

